package com.csz.controller;

import com.csz.Vo.ResultVO;
import com.csz.Vo.UserVo;
import com.csz.mapper.UserMapper;
import com.csz.pojo.User;
import com.csz.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用于验证登录
     * @param username
     * @param password
     * @param session
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/login1")
    public ModelAndView Login(String username , String password, HttpSession session, HttpServletResponse response) throws IOException {
        User user = userService.login(username, password);
        //System.out.println("user===="+user);
        ModelAndView mv = new ModelAndView();
        if (user==null){
            return new ModelAndView("redirect:/loginErro");

        }else{
            session.setAttribute("username",user.getuTruename());
            session.setAttribute("un",user);
            mv.setViewName("index");
            System.out.println("mv===="+mv);
            System.out.println(session.getAttribute("username"));
            return mv;
        }

    }


    /**
     * 验证用户和密码正确
     * @param resp
     * @throws IOException
     */
    @RequestMapping("/loginErro")
    public void erro(HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        // 用弹窗的形式，告诉用户错误
        PrintWriter writer = resp.getWriter();
        String alert = "<script>location.href='login.html';alert('用户或密码不正确')</script>";
        writer.print(alert);
    }

    /**
     * 修改密码错误
     * @param resp
     * @throws IOException
     */

    @RequestMapping("/modifyErro")
    public void modifyerro(HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        // 用弹窗的形式，告诉用户错误
        PrintWriter writer = resp.getWriter();
        String alert = "<script>alert('输入有误 请重新输入')</script>";
        writer.print(alert);
    }


    /**
     * 用户退出登录
     * @param session
     * @return
     */
    @RequestMapping("/outlogin")
    public String outLogin(HttpSession session){
        //1. 清空session
        session.invalidate();
        System.out.println("这个是springmvc的退出功能");
        //2.跳回首页 重定向到login页面
        return "redirect:/login";
    }


    /**
     * 用户修改密码
     * @param password
     * @param newPassword1
     * @param newPassword2
     * @param session
     * @return
     */
    @RequestMapping("/modifyPassword")
    public String modify(String password,String newPassword1,String newPassword2,HttpSession session){
        System.out.println("输入的密码"+password);
        System.out.println("密码1="+newPassword1);
        System.out.println("密码2="+newPassword2);
        User un =(User) session.getAttribute("un");
        Integer id = un.getuId();
        System.out.println(un.getuId());
        System.out.println("un数据库中的密码"+un.getuPassword());

        System.out.println(password.equals(un.getuPassword()));
        System.out.println(newPassword1 == newPassword2);
        if (password.equals(un.getuPassword()) && newPassword1.equals(newPassword2)){
            // 进行修改并跳转到登录页面
            userService.updatePassword(id, newPassword1);
            System.out.println("修改成功");
            return "redirect:/outlogin";
        }else {

            return "forward:/modifyErro";
        }

    }

    @ResponseBody
    @RequestMapping("/getList")
    public ResultVO<User> TeamQuery(Integer pageNum, Integer pageSize, UserVo vo){
        if (pageNum==null || pageNum<=0){
            pageNum = 1;
        }
        if (pageSize==null || pageSize<=0){
            pageSize = 5;
        }
        PageInfo<User> userPageInfo = userService.getUses(pageNum, pageSize, vo);
        System.out.println(new  ResultVO<>(userPageInfo));
        return new  ResultVO<>(userPageInfo);
    }
}
